<template>
	<view>
		<view class="task-bar">
			<input class="task-bar-input" type="text" :value="tasktitle" @input="handleInputChange" placeholder="输入任务" />
			<button class="task-bar-btn" type="primary" @click="addTask">添加任务</button>
		</view>
		<view class="task-list">
			<view class="task-item" v-for=" (item,index) in projectList" :key="index">
				<view class="task-item-cbx">
					<checkbox-group @change="changeState" :data-id="item.id" :data-checked="item.done">
						<checkbox :checked="item.done" />
					</checkbox-group>

				</view>
				<view class="task-item-title">
					{{item.tasktitle}}
				</view>
				<view class="task-item-opration">
					<button type="default" :data-id="item.id" @click="removeTask">删除</button>
				</view>

			</view>
			<view class="task-opration-bar">
				<view class="task-count">{{unDoneLength}}剩余</view>
				<view class="task-btn-group">
					<button size="mini" :type="key==='all' ? 'primary':'default'" @click="all('all')">全部</button>
					<button size="mini" :type="key==='undone' ? 'primary':'default'" @click="all('undone')">未完成</button>
					<button size="mini" :type="key==='done' ? 'primary':'default'" @click="all('done')">完成</button>
				</view>
				<view class="task-clear-btn">
					<button size="mini" type="default" @click="clean">清除已完成</button>
				</view>
			</view>

		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations,
		mapActions,
		mapGetters
	} from 'vuex'
	export default {
		data() {
			return {
				task: ""
			}
		},
		computed: {
			...mapState(['projects', 'tasktitle','key']),
			...mapGetters(['unDoneLength','projectList'])
		},
		methods: {
			handleInputChange(e) {
				//console.log(e.detail.value);
				this.$store.commit('setTaskContent', e.detail.value);
			},
			//添加新任务
			addTask() {
				if (this.$store.state.tasktitle.length > 0) {
					this.$store.commit('addTaskItem')

				} else {
					console.log('task的值为空');
					return;
				}
			},
			removeTask(e) {
				const {
					id
				} = e.currentTarget.dataset;
				console.log(id);
				this.$store.commit('removeTaskItem', id);

			},
			changeState(e) {
				const {
					checked,
					id
				} = e.currentTarget.dataset;
				console.log(e.currentTarget.dataset);
				let param = {
					id: id,
					status: !checked
				}
				this.$store.commit('changeChecked', param);


			},
			clean() {
				this.$store.commit('cleanDone');
			},
			all(status) {
				console.log(status);
				this.$store.commit('changeStatus',status);
			}

		}
	}
</script>

<style>
	.task-bar {
		display: flex;
		margin-top: 10upx;
	}

	.task-bar-input {
		flex: 1;
		height: 70upx;
	}

	.task-bar-btn {
		height: 70upx;
		line-height: 70upx;
	}

	.task-item {

		display: flex;
		border: 1upx solid #C8C7CC;
		height: 100upx;
		line-height: 100upx;
		margin-top: 20upx;
	}

	.task-item-cbx {
		flex: 1;
		display: flex;
		height: 100upx;
		justify-content: center;
		align-items: center;


	}

	.task-item-title {
		flex: 5;
		height: 100upx;
		line-height: 100upx;
	}

	.task-item-opration {
		flex: 2;
		display: flex;
		height: 100upx;
		justify-content: center;
		align-items: center;
	}

	.task-item-opration button {
		height: 60upx;
		line-height: 60upx;
	}

	.task-opration-bar {
		display: flex;
		justify-content: space-between;
		margin-top: 20upx;

	}

	.task-count,
		{
		flex: 1;
		display: flex;
		justify-content: flex-start;
	}

	.task-clear-btn {
		flex: 3;
		display: flex;
		justify-content: flex-end;

	}

	.task-btn-group {
		flex: 5;
		display: flex;
		justify-content: center;

	}
</style>
